package org.iabako.client.init;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import org.iabako.client.navigation.FailureController;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.navigation.URLByPassAccess;
import org.iabako.client.ui.constant.UIConstantProvider;
import org.iabako.client.ui.layout.template.IabakoRootPage;
import org.iabako.client.ui.layout.template.extranet.ExtranetRootPage;
import org.iabako.client.ui.layout.template.nocredit.NoCreditRootPage;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.security.AuthenticateServiceAsync;
import org.iabako.shared.tools.GenericTools;

/**
 * Date: 15/09/13
 * Time: 20:40.
 */
public class Iabako implements EntryPoint {


    public void onModuleLoad() {

        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
            public void onUncaughtException(Throwable e) {
                new FailureController().handleFailure(e, true, true);
            }
        });

        UIConstantProvider.init();
        showHomePage();
    }

    public static void showHomePage() {
        RootPanel.get().clear();
        AuthenticateServiceAsync.Util.getInstance().getAuthenticatedUser(new AsyncCallback<User>() {
            public void onFailure(Throwable caught) {
                //TODO
            }

            public void onSuccess(User user) {
                NavigationController.getInstance();
                boolean userHasCredit = false;
                if (user == null) {
                    //public access
                    NavigationController.getInstance().setRootPage(new ExtranetRootPage());
                    RootPanel.get().add(NavigationController.getInstance().getRootPage());

                    String navigator = Window.Navigator.getUserAgent().toLowerCase();
                    if (navigator.contains("msie") || navigator.contains("trident")) {
                        NavigationController.getInstance().show(new IabakoDialogBox("ie_detected_error_title",
                                "ie_detected_error_text", true, "ie_detected_error_ok"));
                    }

                } else if (user.getUserCredit() == null
                        || GenericTools.getDateFromString(user.getUserCredit().getExpirationDateString()).
                        compareTo(GenericTools.getDateWithoutTime()) < 0) {
                    //no credit access
                    AuthenticateUserProvider.init(user);
                    NavigationController.getInstance().setRootPage(new NoCreditRootPage());
                    RootPanel.get().add(NavigationController.getInstance().getRootPage());

                } else {
                    //private access
                    userHasCredit = true;
                    AuthenticateUserProvider.init(user);
                    NavigationController.getInstance().setRootPage(new IabakoRootPage());
                    RootPanel.get().add(NavigationController.getInstance().getRootPage());
                }

                URLByPassAccess.urlByPassAccess(user == null || !userHasCredit);
            }
        });
    }
}
